/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proy1sistdistribuidos.control.advertisements;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Enumeration;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.Document;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.StructuredDocumentUtils;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.TextElement;
import net.jxta.protocol.PipeAdvertisement;

/**
 * Clase creada por el monitor del curso de
 * Sistemas distribuidos
 * Periodo 2012-1
 * @author alvar-go@uniandes.edu.co
 */
public class MyAdvertisementRecurso extends AdvertisementRecurso{

    private final static String mimeType= "text/xml";

    public static final String nombreAdvertisementTag = "InfoNodo";

    public static final String ipTag = "direccionIP";

    public static final String nombreNodoTag = "NombreNodo";

    /**
     * Elementos que se usan para indexar el advertisement
     */
    private final static String[] fields = {nombreAdvertisementTag};

    public MyAdvertisementRecurso(InputStream  stream) throws IOException{
        super();

        StructuredTextDocument document= (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument( new MimeMediaType(mimeType), stream);
        readAdvertisement(document);
    }

    public MyAdvertisementRecurso(Element document)
    {
        super();
        readAdvertisement((TextElement)document);
    }

    public Document getDocument(MimeMediaType asMimeType) throws IllegalArgumentException
    {
        if((null!= getNombreAdvertisement()) && (null!= getPipeAdv()))
        {
            StructuredDocument document= (StructuredDocument) StructuredDocumentFactory.newStructuredDocument(asMimeType, getAdvertisementType());
            Element element;

            //añadir hijos al documento:

            element= document.createElement(nombreAdvertisementTag, getNombreAdvertisement());
            document.appendChild(element);

            element= document.createElement(ipTag, getDireccionIP());
            document.appendChild(element);

            element= document.createElement(nombreNodoTag, getNombreNodo());
            document.appendChild(element);

            PipeAdvertisement pipe = getPipeAdv();
            if(pipe!=null){
                StructuredTextDocument advDoc = (StructuredTextDocument) pipe.getDocument(asMimeType);
                StructuredDocumentUtils.copyElements(document, document, advDoc);
            }
            return document;
        }
        else{
            throw new IllegalArgumentException("faltan datos para hacer advert");
        }
    }

    public void readAdvertisement(TextElement document)throws IllegalArgumentException
    {
        if(document.getName().equals(getAdvertisementType()))
        {
            Enumeration elements= document.getChildren();

            while(elements.hasMoreElements())
            {
                TextElement element= (TextElement) elements.nextElement();

                if(element.getName().equals(PipeAdvertisement.getAdvertisementType()))
                {
                    PipeAdvertisement pipe = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(element);
                    setPipeAdv(pipe);
                    continue;
                }
                if(element.getName().equals(nombreAdvertisementTag)){
                    setNombreAdvertisement(element.getTextValue());
                    continue;
                }
                if(element.getName().equals(ipTag)){
                    setDireccionIP(element.getTextValue());
                    continue;
                }
                if(element.getName().equals(nombreNodoTag)){
                    setNombreNodo(element.getTextValue());
                    continue;
                }
            }
        }
        else{
            throw new IllegalArgumentException("No Corresponde con el tipo de advertisement esperado");
        }
    }

    public String toString()
    {
        try {
            StringWriter out = new StringWriter();
            StructuredTextDocument doc = (StructuredTextDocument) getDocument(new MimeMediaType(mimeType));
            doc.sendToWriter(out);
            return out.toString();
        } catch (IOException ex) {
            System.out.println("====== ERROR ======");
            System.out.println(ex.getMessage());
            return "";
        }
    }
    public MyAdvertisementRecurso() {
       super();
    }

    @Override
    public String[] getIndexFields() {
        return fields;
    }

    public static class Instantiator implements AdvertisementFactory.Instantiator{

        public String getAdvertisementType() {
           return AdvertisementRecurso.getAdvertisementType();
        }

        public Advertisement newInstance() {
            return new MyAdvertisementRecurso();
        }

        public Advertisement newInstance(Element root) {
            return new MyAdvertisementRecurso(root);
        }

    };
}
